package com.umeng.soexample;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.appcompat.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dankegongyu.lib.share.DKShareCallback;
import com.dankegongyu.lib.share.DKShareUtil;
import com.dankegongyu.lib.share.DK_SHARE_MEDIA;
import com.dankegongyu.lib.share.bean.DKShareWeb;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2018/8/2 14:14
 */
public class ShareDialog extends AlertDialog {

    private Context mContext;

    public static void launch(FragmentActivity activity) {
        new ShareDialog(activity).show();
    }

    protected ShareDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    private void init() {
        setTitle("自定义分享");

        initShareBoard();
    }

    private void initShareBoard() {
        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_share, null);
        setView(view);
        GridView gridView = view.findViewById(R.id.listView);
        final List<ShareBean> data = new ArrayList<>();
        data.add(new ShareBean(DK_SHARE_MEDIA.WEIXIN.getName(), "微信", R.mipmap.ic_launcher));
        data.add(new ShareBean(DK_SHARE_MEDIA.WEIXIN_CIRCLE.getName(), "朋友圈", R.mipmap.ic_launcher_round));
        data.add(new ShareBean(ShareBean.PLATFORM_MORE, "自定义", android.R.drawable.ic_menu_add));
        ShareAdapter shareAdapter = new ShareAdapter(data);
        gridView.setAdapter(shareAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                share(data.get(position));
            }
        });
    }

    private void share(ShareBean shareBean) {
        if (shareBean == null) {
            return;
        }
        if (ShareBean.PLATFORM_MORE.equals(shareBean.platform)) {
            dismiss();
            Toast.makeText(mContext, "自定义操作", Toast.LENGTH_SHORT).show();
        } else {
            final FragmentActivity activity = (FragmentActivity) mContext;
            DKShareWeb shareWeb = DKShareWeb.with(activity)
                    .setTitle("分享友盟")
                    .setSubTitle("说一下你为什么愿意分享友盟~")
                    .setShareUrl(Constants.SHARE_URL)
                    .setShareTitle(Constants.SHARE_TITLE)
                    .setShareImageUrl(Constants.SHARE_THUMB)
                    .setShareImageIcon(R.drawable.ic_share);

            DKShareUtil.share(shareBean.platform, shareWeb, new DKShareCallback() {
                @Override
                public void onStart(String platform) {
                    Toast.makeText(activity, "开始分享：" + platform, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResult(String platform, boolean success) {
                    Toast.makeText(activity, "分享结果：" + platform + " " + success, Toast.LENGTH_SHORT).show();
                    if (success) {
                        dismiss();
                    }
                }
            });
        }
    }

    private static class ShareAdapter extends BaseAdapter {

        private List<ShareBean> mList;

        public ShareAdapter(List<ShareBean> list) {
            this.mList = list;
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_share_item, null);
                holder = new ViewHolder();
                holder.textView = convertView.findViewById(R.id.textView);
                holder.imageView = convertView.findViewById(R.id.imageView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ShareBean shareBean = mList.get(position);
            holder.textView.setText(shareBean.shareText);
            holder.imageView.setImageResource(shareBean.shareIcon);
            return convertView;
        }

        static class ViewHolder {
            private TextView textView;
            private ImageView imageView;
        }
    }

    private static class ShareBean {

        public static final String PLATFORM_MORE = "MORE";

        public String platform;
        public String shareText;
        public int shareIcon;

        public ShareBean(String platform, String shareText, int shareIcon) {
            this.platform = platform;
            this.shareText = shareText;
            this.shareIcon = shareIcon;
        }
    }
}
